// pages/collection/collection.js
var common = require("../../public");
var url = common.url;
var APP_ID = common.APP_ID;
var md5 = common.md5Start;
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  datalist:[1,1,1,1,1]
  },
	// 跳转商品详情
	go_gooddetail:function(e){
		var id = e.currentTarget.dataset.id;
		wx.navigateTo({
			url: '../gooddetail/gooddetail?id='+id,
		})
	},


    /**
     * 倒计时
     */
	setCountDown: function () {
		let timer_hot = this.data.timer_hot;
		if (timer_hot) {
			clearTimeout(timer_hot);
		}
		let time = 1000;
		let {
			listData
		} = this.data;
		let list = listData.map((v, i) => {
			if (v.start_time) {
				if (v.start_time <= 0) {
					v.start_time = 0;
					return;
				}
				let formatTime = this.getFormat(v.start_time);
				v.start_time -= time;
				v.countDown = `${formatTime.hh}:${formatTime.mm}:${formatTime.ss}`;

			} else {
				v.countDown = '00:00:00'; //============做判断   如果没有倒计时的话
			}
			return v;

		})
		this.setData({
			listData: list
		});
		timer_hot = setTimeout(this.setCountDown, time);
		this.setData({
			timer_hot: timer_hot
		})
	},

    /**
     * 格式化时间
     */
	getFormat: function (msec) {
		let ss = parseInt(msec / 1000);
		let ms = parseInt(msec % 1000);
		let mm = 0;
		let hh = 0;
		if (ss > 60) {
			mm = parseInt(ss / 60);
			ss = parseInt(ss % 60);
			if (mm > 60) {
				hh = parseInt(mm / 60);
				mm = parseInt(mm % 60);
			}
		}
		ss = ss > 9 ? ss : `0${ss}`;
		mm = mm > 9 ? mm : `0${mm}`;
		hh = hh > 9 ? hh : `0${hh}`;
		return {
			ms,
			ss,
			mm,
			hh
		};
	}, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	  var that = this;
	  this.setData({
		  page:1
	  })
	  var page=this.data.page;//获取页码
	  var session_str = wx.getStorageSync('session_str');//获取密钥
	  if (!session_str){
		  wx.navigateTo({
			  url: '../author/author',
		  })
	  }
	//  请求收藏接口
	  wx.request({
		  url: url + 'User/getCollection',
		  data: {
			  session_str: session_str,
			  page: page,
			  APP_ID: APP_ID,
			  sign: md5([
				  "session_str" + session_str,
				  "pge"+page ,
				  "APP_ID" + APP_ID,
			  ])
		  },
		  header: {},
		  method: 'post',
		  dataType: 'json',
		  responseType: 'text',
		  success: function (res) {
			  wx.hideLoading();
			  if (res.data.code == 200) {
				  that.setData({
					  listData:res.data.data
				  })
				  that.setCountDown();
			  } else {
				  wx.showToast({
					  title: res.data.msg,
					  icon: 'none',
					  mask: true,
				  })
			  }

		  },
		  fail: function (res) {
			  wx.hideLoading();
			  wx.showToast({
				  title: '网络错误',
				  icon: 'none',
				  mask: true,
			  })
		  }
	  });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})